package com.gdpu.project.service.impl;

import java.util.List;

import com.gdpu.page.vo.QueryResult;
import com.gdpu.project.dao.EmailDao;
import com.gdpu.project.dao.NewsDao;
import com.gdpu.project.dao.TeacherDao;
import com.gdpu.project.service.TeacherService;
import com.gdpu.project.vo.Email;
import com.gdpu.project.vo.News;
import com.gdpu.project.vo.Teacher;

public class TeacherServiceImpl implements TeacherService {
	private TeacherDao teacherDao;
	private EmailDao emailDao;
	private NewsDao newsDao;
	public TeacherDao getTeacherDao() {
		return teacherDao;
	}
	public void setTeacherDao(TeacherDao teacherDao) {
		this.teacherDao = teacherDao;
	}
	public EmailDao getEmailDao() {
		return emailDao;
	}
	public void setEmailDao(EmailDao emailDao) {
		this.emailDao = emailDao;
	}
	public NewsDao getNewsDao() {
		return newsDao;
	}
	public void setNewsDao(NewsDao newsDao) {
		this.newsDao = newsDao;
	}
	public Boolean addTeacher(Teacher teacher ){
		if(teacher!=null){
			teacherDao.save(teacher);
			return true;
		}
		return false;
	}
	public Boolean updateTeacher(Teacher teacher) {
		if(teacher!=null){
			teacherDao.merge(teacher);
			return true;
		}
		return false;
	}
	public Boolean deleteTeacher(Teacher teacher) {
		if(teacher!=null){
			teacherDao.delete(teacher);
			return true;
		}
		return false;
	}
	
	public Boolean checkTeacherLogin(String teacherId, String password,
			Integer role) {
		Teacher teacher=null;
		if(teacherId!=null){
			teacher= teacherDao.findById(teacherId);
		}
		if(teacher!=null){
			if(teacher.getPassword().equals(password)&&teacher.getRole().equals(role)){
				return true;
			}
			return false;
		}
		return false;
	}
	public Teacher findTeacherById(String teacherId) {
		return teacherDao.findById(teacherId);
	}
	
	public int countOfTeacher(int role){
		List<Teacher> teacherlist = teacherDao.findByRole(role);
		if(teacherlist.size()>0){
			return teacherlist.size();
		}
		return 0; 
	}
	
	public List<Teacher> findAllTeacher(int role){
		return teacherDao.findByRole(role);
		}
	
	public Boolean addEmail(Email email) {
		if(email!=null){
			emailDao.save(email);
			return true;
		}
		return false;
	}
	
	public Boolean deleteEmailById(int emailId){
		Email email = emailDao.findById(emailId);
		if(email!=null){
			emailDao.delete(email);
			return true;
		}else{
			return false;
		}
		
	}

	public List<Email> findEmailByReceiver(Teacher teacherId) {
		List<Email> emaillist = emailDao.findByReceiver(teacherId);
		if(emaillist.size()>0){
			return emaillist;
		}
		return null; 
	}
	public List<Email> findEmailBySender(Teacher teacherId) {
		List<Email> emaillist = emailDao.findBySender(teacherId);
		if(emaillist.size()>0){
			return emaillist;
		}
		return null;
	}
	public QueryResult<Email> findEmailBySender(String teacherId,int firstindex,int maxresult) {
		QueryResult<Email> queryResult = emailDao.findBySender(teacherId,firstindex,maxresult);		
		return queryResult;
	}
	public QueryResult<Email> findEmailByReceiver(String teacherId,int firstindex,int maxresult) {
		QueryResult<Email> queryResult = emailDao.findByReceiver(teacherId,firstindex,maxresult);		
		return queryResult;
	}
	
	public int countOfEmail(){
		List<Email> emaillist = emailDao.findAll();
		if(emaillist.size()>0){
			return emaillist.size();
		}else{
			return 0;
		}
	}
	
	public void addClickRateById(int newsid) {
		News news = newsDao.findById(newsid);
		if(news!=null){
		 news.setClickRate(news.getClickRate()+1);
		 newsDao.save(news);
		}
	}
	public Boolean addNews(News news) {
		if(news!=null){
			newsDao.save(news);
			return true;
		}
		return false;
	}
	public Boolean deleteNewsById(int newsId) {
		News news = newsDao.findById(newsId);
		if(news!=null){
			newsDao.delete(news);
			return true;
		}
		return false;
	}
	public List<News> findAllNews() {
		List<News> newslist = newsDao.findAll();
		if(newslist.size()>0){
			return newslist;
		}
		return null;
	}
	public QueryResult<News> findAllNews(int firstindex,int maxresult) {
		QueryResult<News> queryResult = newsDao.findAll(firstindex,maxresult);
		return queryResult;
	}
	public Email findEmailById(int emailId) {
		Email email = emailDao.findById(emailId);
		if(email!=null){
			return email;
		}
		return null;
	}
	public News findNewsById(int newsId) {
		News news = newsDao.findById(newsId);
		if(news!=null){
			return news;
		}
		return null;
	}
	public List<News> findRecentNews() {
		List<News> newslist = newsDao.findRecentNews();
		if(newslist.size()>0){
			return newslist;
		}
		return null;
	}
	public Boolean updateNews(News news) {
		if(news!=null){
			newsDao.merge(news);
			return true;
		}
		return false;
	}
	public int countOfNews() {
		List<News> newslist = newsDao.findAll();
		if(newslist.size()>0){
			return newslist.size();
		}
		return 0;
	}
	
	public List<News> splitPage(int pageNo) {
		int startRow = (pageNo - 1) * 10;
		List<News> pageNews = newsDao.splitPage(startRow);
		return pageNews;
	}

	public List<Email> splitPageEmail(int pageNo) {
		int startRow = (pageNo - 1) * 10;
		List<Email> pageNews = emailDao.splitPage(startRow);
		return pageNews;
	}
	public List<Teacher> findByName(String name){
		List<Teacher> teacherList=teacherDao.findByName(name);
		return teacherList;
	}
	
}
